package com.v2future.sjt.collegecheckingattendance.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import com.v2future.sjt.collegecheckingattendance.MainApplication;


/**
 * 优化Toast，功能防止用户短时间内多次触发Toast而发生的Toast泛滥.
 *
 * @author sjt
 */
public class ToastUtil {

	/** The toast. */
	private static Toast toast = null;

	/**
	 * Instantiates a new toast util.
	 */
	private ToastUtil() {
	}

	private static Toast getInstance(Context context, int resStr, int during) {

		return getInstance(context, context.getString(resStr), during);

	}

	/**
	 * Gets the single instance of ToastUtil.
	 *
	 * @param context
	 *            the context
	 * @param msg
	 *            the msg
	 * @param during
	 *            the during
	 * @return single instance of ToastUtil
	 */
	@SuppressLint("ShowToast")
	private static Toast getInstance(Context context, String msg, int during) {
		if (toast == null) {
			toast = Toast.makeText(context, msg, during);

		}
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.setDuration(during);
		toast.setText(msg);
		return toast;
	}

	/**
	 * Show message.
	 *
	 * @param context
	 *            the context
	 * @param msg
	 *            the msg
	 * @param during
	 *            the during
	 */
	public static void showMessage(final Context context, final String msg,
								   int during) {

		getInstance(MainApplication.getInstanceContext(), msg, during).show();

	}

	public static void showMessage(final Context context, final int resStr,
								   int during) {

		getInstance(MainApplication.getInstanceContext(), resStr, during)
				.show();

	}

	public static void showMessage(final Context context, final int resStr) {

		getInstance(MainApplication.getInstanceContext(), resStr,
				Toast.LENGTH_SHORT).show();

	}

	public static void showMessage(final String resStr) {

		getInstance(MainApplication.getInstanceContext(), resStr,
				Toast.LENGTH_SHORT).show();

	}

}